############################################################################################################################################
# The Cultural Origins of Populism ----------------------------
# Yotam Margalit, Shir Raviv and Omer Solodoch  ---------------------------
# This R file contains the code necessary to replicate the analysis of Figure SI-20 in the main text ----------------------------
############################################################################################################################################

#### Clear environment and return memory to the operating system ---------
rm(list = ls())
gc()

#### Installing and loading needed packages -----------
# Load or install the required packages for this analysis
packages_required <- c(
  'stargazer', 'lmtest', 'haven', 'expss', 'dplyr', 'rmarkdown', 
  'psych', 'Hmisc','base','haven','broom',
  'ivmte', 'ggpubr', 'writexl', 'rio', 'patchwork', 'Lahman', 'cregg', 'xtable', 'ggmosaic'
)

# Function to check and install any missing packages
install_and_load_packages <- function(packages) {
  for (package in packages) {
    if (!require(package, character.only = TRUE)) {
      install.packages(package)
      library(package, character.only = TRUE)
    }
  }
}

# Apply the function to the list of required packages
install_and_load_packages(packages_required)


# Set Working Directory  ############
# Set the working directory where the data is stored and where to save outputs
setwd("insert here.. /JOP Replication")
plotpath<-"figures/"

#### Data ---------
data <- readRDS("data/ANES_2020_raw_data.rds")

# Trump Vote ----
data$TrumpVoter<-0
data$TrumpVoter[data$V202073 ==2] <-1
data$TrumpVoter[data$V202073 ==1] <-0 #Joe Biden
data$TrumpVoter[data$V202073 ==3 | data$V202073 ==4 | data$V202073 ==5] <-0 # Jo Jorgensen/Howie Hawkins/Other candidate 
data$TrumpVoter[data$V202073 >5] <-NA
data$TrumpVoter[data$V202073 <0] <-NA


data$TrumpVoter2016<-0
data$TrumpVoter2016[data$V201103 ==2] <-1 #  2. Donald Trump
data$TrumpVoter2016[data$V201103 ==1] <-0 #  1. Hillary Clinton
data$TrumpVoter2016[data$V201103 ==5] <-0 #  Other
data$TrumpVoter2016[data$V201103 ==3 | data$V201103 ==4 | data$V201103 ==5] <-0 # Jo Jorgensen/Howie Hawkins
data$TrumpVoter2016[data$V201103 >5] <-NA
data$TrumpVoter2016[data$V201103 <0] <-NA


# Rural Resentment ----
# Urban_level
data$Urban_level <- NA
data$Urban_level[data$V202355 == 1] <- 1 # Rural area
data$Urban_level[data$V202355 == 2] <- 2
data$Urban_level[data$V202355 == 3] <- 3
data$Urban_level[data$V202355 == 4] <- 4

# rural_suburb_d
data$rural_suburb_d <- NA
data$rural_suburb_d[data$V202355 >= 1 & data$V202355 <= 3] <- 1
data$rural_suburb_d[data$V202355 == 4] <- 0
table(data$rural_suburb_d )

# rural
data$rural <- NA
data$rural[data$V202355 ==1] <- 1
data$rural[data$V202355 >1] <- 0
table(data$rural )

# rural_smalltown_d
data$rural_smalltown_d <- NA
data$rural_smalltown_d[data$V202355 ==1 | data$V202355 ==2] <- 1
data$rural_smalltown_d[data$V202355 >2] <- 0
table(data$rural_smalltown_d )


data$rural_govt_less <- NA
data$rural_govt_less2 <- NA
data$rural_resentment <- NA

# Recode V202276x to create rural_govt_less
data$rural_govt_less[data$V202276x >= 1 & data$V202276x <= 4] <- 0  # "more/same"
data$rural_govt_less[data$V202276x >= 5 & data$V202276x <= 7] <- 1  # "less"

# Recode V202276x to create rural_govt_less2
data$rural_govt_less2[data$V202276x >= 1 & data$V202276x <= 5] <- 0  # "more/same/little less"
data$rural_govt_less2[data$V202276x >= 6 & data$V202276x <= 7] <- 1  # "less"


# Recode V202212 to create rural_gov_dontcare
# Public officials don't care what people think
data$rural_gov_dontcare[data$V202212 >= 1 & data$V202212 <= 2] <- 1  # "Agree strongly"
data$rural_gov_dontcare[data$V202212 >= 3 & data$V202212 <= 5] <- 0  # "Agree strongly"


# Generate rural_resentment_original based on rural_govt_less and rural
data$rural_resentment_original <- 0
data$rural_resentment_original[data$rural_gov_dontcare==1 & data$rural==1]<-1
data$rural_resentment_original[is.na(data$rural_gov_dontcare) | is.na(data$rural)]<-NA


# Generate rural_resentment_specific based on rural_govt_less and rural
data$rural_resentment_specific <- 0
data$rural_resentment_specific[data$rural_govt_less==1 & data$rural==1]<-1
data$rural_resentment_specific[is.na(data$rural_govt_less) | is.na(data$rural)]<-NA

# Community disintegration ----
data$community_nocoop <- NA  
data$community_nocoop[data$V202031 == 2] <- 1
data$community_nocoop[data$V202031 == 1] <- 0

data$community_nomeet <- NA  
data$community_nomeet[data$V202032 == 2] <- 1
data$community_nomeet[data$V202032 == 1] <- 0

data$community_lowcontact <- 0  
data$community_lowcontact[data$community_nomeet == 1 & data$community_nocoop==1] <- 1
data$community_lowcontact[is.na(data$community_nomeet)|
                        is.na(data$community_nocoop)] <- NA

data$community_lowcontact3 <-data$community_lowcontact
data$community_lowcontact3[data$V202038 == 1] <- 0
table(data$community_lowcontact3)


# community_lowcontact3
data$community_lowcontact3 <- data$community_lowcontact
data$community_lowcontact3[data$V202038 == 1] <- 0
table(data$community_lowcontact3)


# Ethnocultural estrangement ---------
# White
data$race1 <- 0
data$race1[is.na(data$V201549x)]<-NA
data$race1[data$V201549x==-8|
             data$V201549x==-9]<-NA
data$race1[data$V201549x==1]<-1


# Native
data$native_2nd1 <- 0
data$native_2nd1[is.na(data$V201553)]<-NA
data$native_2nd1[data$V201553==-8|
                   data$V201553==-9]<-NA
data$native_2nd1[data$V201553==1 |
                   data$V201553==2]<-1
table(data$native_2nd1)


# white_native
data$white_native <- 0
data$white_native[data$native_2nd1==1 &
                    data$race1==1] <-1 
data$white_native[is.na(data$native_2nd1)]<-NA
data$white_native[is.na(data$race1)]<-NA
table(data$native_2nd1)



# img_undermined_cult
data$img_undermined_cult_5 <- NA
data$img_undermined_cult <- NA
data$img_str_undermined_cult <- NA

data$img_undermined_cult_5[data$V202419 >= 1 & data$V202419 <= 5] <- 6 - data$V202419[data$V202419 >= 1 & data$V202419 <= 5]
data$img_undermined_cult[data$V202419 >= 3 & data$V202419 <= 5] <- 0  # "Disagree/neither"
data$img_undermined_cult[data$V202419 >= 1 & data$V202419 <= 2] <- 1  # "Agree"

data$img_str_undermined_cult[data$V202419 >= 2 & data$V202419 <= 5] <- 0  # "other"
data$img_str_undermined_cult[data$V202419 == 1] <- 1  # "Agree strongly"
table(data$img_undermined_cult_5)

# img_undermined_cult
data$White_Native_img_undermined_cult <- 0
data$White_Native_img_undermined_cult[data$img_undermined_cult==1 &
                            data$white_native==1] <-1 
data$White_Native_img_undermined_cult[is.na(data$img_undermined_cult)]<-NA
data$White_Native_img_undermined_cult[is.na(data$white_native)]<-NA



# Intergenerational backlash ---------
data$age <- NA  
data$age[data$V201507x != -9] <- data$V201507x[data$V201507x != -9]
# birthyear
data$birthyear <- 2020 - data$age

# age_55
data$age_55 <- NA
data$age_55[data$age >= 0 & data$age <= 54] <- 0  # "18-54"
data$age_55[data$age >= 55 & data$age <= 120] <- 1  # "55+"

data$child_respect <- NA
data$child_manners <- NA
data$child_obedience <- NA
data$child_wellbehaved <- NA

#child_respect
data$child_respect[data$V202266 == 1] <- 1  # "Independence"
data$child_respect[data$V202266 == 3] <- 2  # "both"
data$child_respect[data$V202266 == 2] <- 3  # "Respect for elders"

# child_manners
data$child_manners[data$V202267 == 1] <- 1  # "Curiosity"
data$child_manners[data$V202267 == 3] <- 2  # "both"
data$child_manners[data$V202267 == 2] <- 3  # "Good manners"

# child_obedience
data$child_obedience[data$V202268 == 2] <- 1  # "Self-reliance"
data$child_obedience[data$V202268 == 3 | data$V202268 == 4] <- 2  # "both/neither"
data$child_obedience[data$V202268 == 1] <- 3  # "Obedience"

# child_wellbehaved
data$child_wellbehaved[data$V202269 == 1] <- 1  # "Being considerate"
data$child_wellbehaved[data$V202269 == 3] <- 2  # "both"
data$child_wellbehaved[data$V202269 == 2] <- 3  # "Well behaved"

# authoritarian_index
data$authoritarian_index <- rowMeans(data[c("child_respect", "child_manners", 
                                            "child_obedience", "child_wellbehaved")], na.rm = TRUE)

data$authoritarian <- NA
data$authoritarian[data$authoritarian_index >= 1 & data$authoritarian_index < 2.5] <- 0  # "non-authoritarian"
data$authoritarian[data$authoritarian_index >= 2.5 & data$authoritarian_index <= 3] <- 1  # "authoritarian"

data$older55_authoritarian <- data$authoritarian
data$older55_authoritarian[data$age_55 == 0] <- 0
table(data$older55_authoritarian)

# Social status ---------
# V202352 POST: HOW WOULD R DESCRIBE SOCIAL CLASS [EGSS]
# V202353 POST: IS R LOWER MIDDLE CLASS, MIDDLE CLASS, UPPER MIDDLE CLASS? [EGSS]
data$social_status_anx <- 0
data$social_status_anx[ is.na(data$V202352)]<-NA
data$social_status_anx[ data$V202352==2]<-1 # Working  class

data$social_status_anx[ data$V202353==1]<-1 # Lower middle class
data$social_status_anx[ is.na(data$V202353)]<-NA



##### Figure (SI-20) Cultural Concerns and Support for Trump, ANES 2020 ------------------------------------#####


Fig_SI20_coef <- data %>%
  do(tidy(lm(TrumpVoter ~                
               White_Native_img_undermined_cult+
               older55_authoritarian+
               rural_resentment_original+social_status_anx+community_lowcontact3+TrumpVoter2016,
             weights = V200010b, data = .)))

Fig_SI20_coef<-Fig_SI20_coef%>% 
  filter(term %in% c("White_Native_img_undermined_cult", 
                     "older55_authoritarian", "rural_resentment_original",
                     "community_lowcontact3", "social_status_anx")) %>%
  mutate(term=case_when(
    term=="White_Native_img_undermined_cult"~"Ethno-Cultural Estrangement",
    term=="older55_authoritarian"~"Intergenerational Backlash",
    term=="rural_resentment_original"~"Rural Resentment",
    term=="social_status_anx"~"Social Status Anxiety",
    term=="community_lowcontact3"~"Community Disintegration"
  )) %>%
  mutate(term = factor(term, levels = c( 
    "Intergenerational Backlash", "Social Status Anxiety", 
    "Rural Resentment",
    "Community Disintegration",
    "Ethno-Cultural Estrangement")))  # Set the order of the factors

Fig_SI20<-
  Fig_SI20_coef %>%  
  ggplot()+aes(term, estimate, fill=term, color=term)+ 
  geom_hline(yintercept=0, linetype=2, lwd=0.9, size=1.3, colour = "#ACBBBF", alpha=0.9) +
  geom_errorbar(stat = "identity", alpha = 0.5, 
                position = position_dodge(width = 0.7),
                aes(ymin=estimate - 1.96*std.error, ymax=estimate + 1.96*std.error),
                lwd=0.8, width = 0)+
  geom_errorbar(stat = "identity", alpha = 0.8, 
                position = position_dodge(width = 0.7),
                aes(ymin=estimate - 1.64*std.error, ymax=estimate + 1.64*std.error), 
                lwd=0.6, width=0) +
  scale_colour_manual(values = c("#96051A","#96051A","#96051A","#96051A","#96051A"))+
  scale_fill_manual(values = c("#96051A","#96051A","#96051A","#96051A","#96051A"))+
  geom_point(stat = "identity", alpha = 0.7,  
             size = 1, position = position_dodge(width = 0.7)) + 
  guides(color = FALSE, fill = FALSE)+
  coord_flip() + 
  labs(x = "", y = " ", subtitle = "", shape="") +
  theme_minimal() + theme(legend.position = "bottom",  
                          strip.text.x = element_text(size = 12),
                          axis.text = element_text(size = 12),
                          axis.text.x = element_text(size = 11),
                          plot.margin = unit(c(0,1, 0.5, 0), "cm"),
                          panel.spacing = unit(1, "lines"))
Fig_SI20


ggsave(Fig_SI20,
       filename = paste0(plotpath, "Fig_SI20.tif"),
       device = "tiff",
       height = 4, width = 5, dpi = 600)

